//
//  MassRecordVC.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/3.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MassRecordVC.h"
#import "MassRecordModel.h"
#import "MassRecordCell.h"
#import "HTTPGlobals.h"
#import "AFHTTPRequestOperation.h"
@interface MassRecordVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataSource;  /**< 存放数据源 */
    NSMutableArray *_cellArr;     /**< 存放cell,用于计算高度的 */
}

@end

@implementation MassRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"群发历史";
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:_tableView];
    [self loadMassRecordModel];
    
}
//加载数据
-(void)loadMassRecordModel {
    _dataSource = [NSMutableArray array];
    //群发历史(需要登录为前提获取)
    [self.HUD show:YES];
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:@"MobilePort/Index/qunfalis.html" parameters:nil finish:^(BOOL seccess, NSString *error, id obj) {
        [self.HUD hide:YES];
        if (seccess) {
            DLog(@"data = %@",obj[@"data"]);
            for (NSDictionary *dic in obj[@"data"]) {
                NSDictionary *parame = [NSDictionary dictionaryWithObjectsAndKeys:dic[@"headsmall"],@"headImage",dic[@"nickname"],@"name",dic[@"actiontime"],@"time",dic[@"content"],@"context", nil];
                MassRecordModel *model = [MassRecordModel statusWithDictionary:parame];
                [_dataSource addObject:model];
            }
            _cellArr = [NSMutableArray array];
            [_dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                MassRecordCell *cell = [[MassRecordCell alloc] init];
                [_cellArr addObject:cell];
            }];
            
            [_tableView reloadData];
        }
    }];
    
}

#pragma mark --tableViewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    MassRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[MassRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    cell.model = _dataSource[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MassRecordCell *cell = _cellArr[indexPath.row];
    cell.model = _dataSource[indexPath.row];
    
    return cell.height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
